home *** CD-ROM | disk | FTP | other *** search
- class CPlayingField extends CMovieClipFresh
- {
- var _shuffler;
- var _arrPieceSetMoves;
- var _travellingPieces;
- var _arrPendingPieceMoveSet;
- var mc_pieceHost;
- var _pieceGrid;
- var mc_pieceHostHost;
- var _arrGroups;
- var _trayBackground;
- var _frame;
- var _trayExtentX = 0;
- var _trayExtentY = 0;
- var _tableauExtentY = 0;
- var _totalExtentY = 0;
- var _nPieces = 0;
- var TILE_WIDTH = 50;
- var _wantsBuilding = true;
- var _isPlayingVictory = false;
- var _nMoves = 0;
- var _curPieceSetMove = -1;
- var _interactive = true;
- var _startTime = 0;
- var _pauseStartTime = 0;
- var _timeSpentNonInteractive = 0;
- function CPlayingField()
- {
- super();
- this._visible = false;
- this._shuffler = new CPieceShuffler();
- this._arrPieceSetMoves = new Array();
- this._travellingPieces = new Array();
- this._arrPendingPieceMoveSet = new Array();
- this._startTime = this.GetTime();
- this.SetInteractive(false);
- this.stop();
- }
- function AddTravellingPiece(piece)
- {
- var _loc2_ = 0;
- while(_loc2_ < this._travellingPieces.length)
- {
- if(piece == this._travellingPieces[_loc2_])
- {
- return false;
- }
- _loc2_ = _loc2_ + 1;
- }
- this._travellingPieces.push(piece);
- return true;
- }
- function RecordTravellingPieceMovement()
- {
- var _loc2_ = 0;
- while(_loc2_ < this._travellingPieces.length)
- {
- CPhitPiece(this._travellingPieces[_loc2_]).RecordLastMove();
- _loc2_ = _loc2_ + 1;
- }
- this.FinalizePieceMoveSet();
- this._travellingPieces.splice(0);
- this._arrPendingPieceMoveSet = new Array();
- }
- function FinalizePieceMoveSet()
- {
- this._arrPieceSetMoves.splice(this._curPieceSetMove + 1,this._arrPieceSetMoves.length - this._curPieceSetMove - 1);
- this._arrPieceSetMoves.push(this._arrPendingPieceMoveSet);
- this._curPieceSetMove = this._arrPieceSetMoves.length - 1;
- this._nMoves = this._nMoves + 1;
- }
- function get _tileWidth()
- {
- return this.TILE_WIDTH;
- }
- function get _trayWidth()
- {
- return this._trayExtentX;
- }
- function get _trayHeight()
- {
- return this._trayExtentY;
- }
- function get _minTilePos()
- {
- return - this._tableauExtentY;
- }
- function get _tableauPixelHeight()
- {
- return this._tableauExtentY * this._tileWidth;
- }
- function get _pixelWidth()
- {
- return this._trayExtentX * this._tileWidth;
- }
- function get _pixelHeight()
- {
- return this._totalExtentY * this._tileWidth;
- }
- function SetInteractive(isInteractive)
- {
- if(this._interactive != isInteractive)
- {
- this._interactive = isInteractive;
- if(this._interactive)
- {
- if(this._pauseStartTime)
- {
- this._timeSpentNonInteractive += this.GetTime() - this._pauseStartTime;
- this._pauseStartTime = 0;
- }
- }
- else
- {
- this._pauseStartTime = this.GetTime();
- }
- var _loc2_ = 0;
- while(_loc2_ < this._nPieces)
- {
- var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
- _loc3_.SetInteractive(this._interactive);
- _loc2_ = _loc2_ + 1;
- }
- }
- }
- function IsInBounds(tilePos)
- {
- return tilePos._x >= 0 && tilePos._y >= - this._tableauExtentY && tilePos._x < this._trayExtentX && tilePos._y < this._trayExtentY;
- }
- function IsInTray(tilePos)
- {
- return tilePos._x >= 0 && tilePos._y >= 0 && tilePos._x < this._trayExtentX && tilePos._y < this._trayExtentY;
- }
- function GetPieceAtTileLocation(tilePos)
- {
- return this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x];
- }
- function SetTileOwner(tilePos, owner)
- {
- FreshDebug.Assert(!!owner,"owner");
- FreshDebug.Assert(!this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x],"_pieceGrid[ tilePos._y * _trayExtentX + tilePos._x ] == null OR undefined");
- this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x] = owner;
- }
- function ClearTileOwner(tilePos)
- {
- this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x] = null;
- }
- function CleanUp()
- {
- this._pieceGrid = null;
- if(this.mc_pieceHost)
- {
- this.mc_pieceHost = null;
- §§push(this.removeMovieClip());
- }
- }
- function Build(trayExtentX, trayExtentY, minGroupSize, maxGroupSize)
- {
- FreshDebug.Trace("Build");
- this.CleanUp();
- this._trayExtentX = trayExtentX;
- this._trayExtentY = trayExtentY;
- this.mc_pieceHostHost.createEmptyMovieClip("mc_pieceHost",0);
- this.mc_pieceHost = this.mc_pieceHostHost.mc_pieceHost;
- FreshDebug.Assert(this.mc_pieceHost != undefined,"mc_pieceHost != undefined");
- if(minGroupSize != undefined && maxGroupSize != undefined)
- {
- var _loc2_ = new CTrayBuilder();
- this._arrGroups = _loc2_.Build(trayExtentX,trayExtentY,minGroupSize,maxGroupSize);
- this.SetGroups(this._arrGroups);
- }
- }
- function BuildPieces(arrGroups)
- {
- this._pieceGrid = new Array();
- var _loc7_ = _root.random.GetRandom() * 360;
- var _loc9_ = 360 / arrGroups.length;
- var _loc3_ = 0;
- while(_loc3_ < arrGroups.length)
- {
- var _loc5_ = this.mc_pieceHost.getNextHighestDepth();
- var _loc6_ = CPhitPiece(this.mc_pieceHost.attachMovie("Piece","piece" + _loc3_,_loc5_));
- var _loc4_ = MathUtil.HSVtoRGB(_loc7_,1,0.75);
- _loc6_.Create(_loc4_.r,_loc4_.g,_loc4_.b,arrGroups[_loc3_]);
- _loc7_ += _loc9_;
- _loc3_ = _loc3_ + 1;
- }
- this._nPieces = arrGroups.length;
- }
- function CreateTableau()
- {
- this._tableauExtentY = Math.ceil(this._trayExtentY * 1.5);
- this._totalExtentY = this._tableauExtentY + this._trayExtentY;
- }
- function AreAllPiecesOutsideOfTray()
- {
- var _loc2_ = 0;
- while(_loc2_ < this._nPieces)
- {
- var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
- if(!_loc3_.IsCompletelyOutsideOfTray())
- {
- return false;
- }
- _loc2_ = _loc2_ + 1;
- }
- return true;
- }
- function AreAllPiecesWithinTray()
- {
- var _loc2_ = 0;
- while(_loc2_ < this._nPieces)
- {
- var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
- if(!_loc3_.IsCompletelyInsideOfTray())
- {
- return false;
- }
- _loc2_ = _loc2_ + 1;
- }
- return true;
- }
- function Shuffle(minIterations, maxIterations, maxStep, upBias)
- {
- if(!this._shuffler)
- {
- return undefined;
- }
- this._shuffler.Shuffle(this._trayExtentX,this._trayExtentY,this._tableauExtentY,this.mc_pieceHost,this._nPieces,maxStep,minIterations,upBias);
- var _loc2_ = minIterations;
- while(!this.AreAllPiecesOutsideOfTray() && _loc2_ < maxIterations)
- {
- this._shuffler.Shuffle(this._trayExtentX,this._trayExtentY,this._tableauExtentY,this.mc_pieceHost,this._nPieces,maxStep,1,upBias);
- _loc2_ = _loc2_ + 1;
- }
- return _loc2_;
- }
- function PlayVictory()
- {
- if(this._isPlayingVictory)
- {
- return undefined;
- }
- FreshDebug.Trace("PlayVictory");
- this._isPlayingVictory = true;
- this.SetInteractive(false);
- this.gotoAndPlay("victory");
- }
- function OnVictoryDone()
- {
- FreshDebug.Trace("OnVictoryDone");
- _root._game.OnVictoryDisplayFinished();
- this.stop();
- }
- function WantsBuilding()
- {
- return this._wantsBuilding;
- }
- function IsAwake()
- {
- return true;
- }
- function SaveTrace(msg)
- {
- FreshDebug.Trace(msg,"field-code");
- }
- function Save(iLevel)
- {
- this.SaveTrace("\tfunction StartCreatingLevel" + iLevel + "() : CPlayingField");
- this.SaveTrace("\t{");
- this.SaveTrace("\t\treturn _root._game.CreatePlayingField( " + iLevel + ", " + this._trayWidth + ", " + this._trayHeight + ", FinishCreatingLevel" + iLevel + " );");
- this.SaveTrace("\t}");
- this.SaveTrace("");
- this.SaveTrace("\tfunction FinishCreatingLevel" + iLevel + "( fieldHost : CFieldHost, width : Number, height : Number ) : CPlayingField");
- this.SaveTrace("\t{");
- this.SaveTrace("\t\tFreshDebug.Trace( \"FinishCreatingLevel" + iLevel + "\" );");
- this.SaveTrace("");
- this.SaveTrace("\t\tvar playingField : CPlayingField = fieldHost._field;");
- this.SaveTrace("\t\tplayingField.Build( width, height );");
- this.SaveTrace("");
- this.SaveTrace("\t\tplayingField.SetGroups(");
- this.SaveTrace("\t\t\t[");
- var _loc6_ = 0;
- while(_loc6_ < this._arrGroups.length)
- {
- var _loc4_ = this._arrGroups[_loc6_];
- var _loc3_ = "\t\t\t\t[";
- var _loc2_ = 0;
- while(_loc2_ < _loc4_.length)
- {
- var _loc5_ = _loc4_[_loc2_];
- _loc3_ += " {_x: " + _loc5_._x + ", _y: " + _loc5_._y + " }";
- if(_loc2_ < _loc4_.length - 1)
- {
- _loc3_ += ",";
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ += " ]";
- if(_loc6_ < this._arrGroups.length - 1)
- {
- _loc3_ += ",";
- }
- this.SaveTrace(_loc3_);
- _loc6_ = _loc6_ + 1;
- }
- this.SaveTrace("\t\t\t]");
- this.SaveTrace("\t\t);");
- this.SaveTrace("");
- var _loc7_ = 0;
- while(_loc7_ < this._nPieces)
- {
- var _loc9_ = CPhitPiece(this.mc_pieceHost["piece" + _loc7_]);
- var _loc8_ = _loc9_._tilePosition;
- this.SaveTrace("\t\tplayingField.PositionPiece( " + _loc7_ + ", " + _loc8_._x + ", " + _loc8_._y + " );");
- _loc7_ = _loc7_ + 1;
- }
- this.SaveTrace("");
- this.SaveTrace("\t\treturn playingField;");
- this.SaveTrace("\t}");
- }
- function SetGroups(arrGroups)
- {
- this._arrGroups = arrGroups;
- this.BuildPieces(arrGroups);
- this.CreateTableau();
- this._trayBackground._x = this._x - 2;
- this._trayBackground._y = this._y - 2;
- this._trayBackground._width = this._tileWidth * this._trayWidth + 4;
- this._trayBackground._height = this._tileWidth * this._trayHeight + 4;
- this._wantsBuilding = false;
- this._visible = true;
- }
- function PositionPiece(iPiece, x, y)
- {
- var _loc2_ = CPhitPiece(this.mc_pieceHost["piece" + iPiece]);
- _loc2_.MoveToPosition(new Vector2D(x,y),true);
- }
- function RememberPieceBasePositions()
- {
- var _loc2_ = 0;
- while(_loc2_ < this._nPieces)
- {
- var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
- _loc3_.RecordBaseSteadyPosition();
- _loc2_ = _loc2_ + 1;
- }
- }
- function RecordPieceMove(piece, startLoc, endLoc)
- {
- var _loc2_ = {piece:piece,startLoc:startLoc,endLoc:endLoc};
- this._arrPendingPieceMoveSet.push(_loc2_);
- }
- function IsUndoAvailable()
- {
- return this._curPieceSetMove >= 0;
- }
- function IsRedoAvailable()
- {
- return this._curPieceSetMove + 1 < this._arrPieceSetMoves.length;
- }
- function UndoLastPieceMove()
- {
- if(this._curPieceSetMove >= 0)
- {
- var _loc4_ = this._arrPieceSetMoves[this._curPieceSetMove--];
- var _loc3_ = 0;
- while(_loc3_ < _loc4_.length)
- {
- var _loc2_ = _loc4_[_loc3_];
- FreshDebug.Assert(_loc2_.piece._tilePosition.IsEqual(_loc2_.endLoc),"move.piece._tilePosition.IsEqual( move.endLoc )");
- _loc2_.piece.MoveToPosition(_loc2_.startLoc,true);
- _loc3_ = _loc3_ + 1;
- }
- }
- }
- function RedoLastPieceMove()
- {
- if(this._curPieceSetMove + 1 < this._arrPieceSetMoves.length)
- {
- var _loc4_ = this._arrPieceSetMoves[++this._curPieceSetMove];
- var _loc2_ = _loc4_.length - 1;
- while(_loc2_ >= 0)
- {
- var _loc3_ = _loc4_[_loc2_];
- _loc3_.piece.MoveToPosition(_loc3_.endLoc,true);
- _loc2_ = _loc2_ - 1;
- }
- }
- }
- function ResetAllMoves()
- {
- while(this._curPieceSetMove >= 0)
- {
- this.UndoLastPieceMove();
- }
- }
- function GetNumMovesMade()
- {
- return this._nMoves;
- }
- function GetTimeTaken()
- {
- if(this._startTime)
- {
- var _loc2_ = this.GetTime();
- return _loc2_ - this._startTime - this._timeSpentNonInteractive - (this._pauseStartTime <= 0 ? 0 : _loc2_ - this._pauseStartTime);
- }
- return undefined;
- }
- function GetTime()
- {
- return new Date().getTime() / 1000;
- }
- function SizeFrame(scale)
- {
- this._frame._x = this._x;
- this._frame._y = this._y - this._tableauPixelHeight - 1;
- this._frame._width = this._pixelWidth + 28 / scale;
- this._frame._height = this._pixelHeight + 4;
- }
- }
-